<?php

namespace app\agent\controller;

use app\models\{Region, PlatSons, GoodsSpec, Order};
use think\Controller;
use think\facade\Env;
use think\Db;
use oss\Oss;
use app\facade\Redis;

class Base extends Controller
{


    public function getalldata()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['businessid'] = $this->request->business ?? [];
            if (empty($data['type'])) {
                return json(['status' => 1, 'code' => 1, 'msg' => '参数信息错误']);
            }
            switch ($data['type']) {
                case '1':
                    //查询24小时内订单数据
                    if (empty($data['time'])) {
                        $startime = date('Y-m-d', time() - 7 * 86400);
                        $endtime = date('Y-m-d');
                    } else {
                        $data['time'] = explode('~', $data['time']);
                        $startime = $data['time'][0] ?? date('Y-m-d', time() - 7 * 86400);
                        $endtime = $data['time'][1] ??  date('Y-m-d');
                    }
                    $d1 = strtotime($startime);
                    $d2 = strtotime($endtime);
                    $Days = round(($d2 - $d1) / 3600 / 24);
                    $datas = [];
                    for ($i = 0; $i <= $Days; $i++) {
                        $datas[$i]['timename'] = date('Y-m-d', $d1 + $i * 86400);
                        $startime =  date('Y-m-d', $d1 + $i * 86400);
                        $endtime =  date('Y-m-d', $d1 + ($i + 1) * 86400);
                        $datas[$i]['type1'] = (new Order())->where(['businessid' => $data['businessid'], 'status' => [2, 3, 4]])->whereBetweenTime('created', $startime, $endtime)->count();
                        $datas[$i]['type2'] = (new Order())->where(['businessid' => $data['businessid'], 'status' => [2]])->whereBetweenTime('created', $startime, $endtime)->count();
                        $datas[$i]['type3'] = (new Order())->where(['businessid' => $data['businessid'], 'status' => [5]])->whereBetweenTime('caceltimes', $startime, $endtime)->count();
                        $datas[$i]['type4'] = (new Order())->where(['businessid' => $data['businessid'], 'tuikuan' => [1, 2, 3, 4]])->whereBetweenTime('caceltimes', $startime, $endtime)->count();
                        $datas[$i]['type5'] = (new Order())->where(['businessid' => $data['businessid'], 'status' => [4]])->whereBetweenTime('finishtimes', $startime, $endtime)->count();
                        $datas[$i]['type6'] = (new Order())->where(['businessid' => $data['businessid'], 'status' => [2, 3, 4]])->whereBetweenTime('created', $startime, $endtime)->sum('total');
                        $datas[$i]['type7'] = (new Order())->where(['businessid' => $data['businessid'], 'status' => [2]])->whereBetweenTime('created', $startime, $endtime)->sum('total');
                        $datas[$i]['type8'] = (new Order())->where(['businessid' => $data['businessid'], 'status' => [5]])->whereBetweenTime('caceltimes', $startime, $endtime)->sum('total');
                        $datas[$i]['type9'] = (new Order())->where(['businessid' => $data['businessid'], 'tuikuan' => [1, 2, 3, 4]])->whereBetweenTime('caceltimes', $startime, $endtime)->sum('total');
                        $datas[$i]['type10'] = (new Order())->where(['businessid' => $data['businessid'], 'status' => [4]])->whereBetweenTime('finishtimes', $startime, $endtime)->sum('total');
                    }
                    return json(['code' => 0, 'status' => 0, 'data' => $datas]);
                    break;
                default:
                    return json(['code' => 1, 'status' => 1, 'data' => []]);
                    break;
            }
            return json(['code' => 0, 'status' => 0, 'data' => []]);
        } else {
            return json(['code' => 1, 'status' => 1, 'data' => []]);
        }
    }

    public function initialize()
    {
        $menu = config('amenu.menu');
        $controller = strtolower($this->request->controller());
        $action = strtolower($this->request->action());
        $path = [];

        if (isset($menu[$controller])) {
            $path['c'] = $menu[$controller]['name'] ?: null;
            $path['a'] = $menu[$controller]['sub'][$action]['name'] ?: null;
            $path['s'] = $menu[$controller]['sub'][$action]['show'] ?: null;
        }
        $this->assign('path', $path);
    }

    protected function get_rand_str($len = 6)
    {
        $arr = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'), array('$', '@', '#', '%', '&'));
        shuffle($arr);
        $sub_arr = array_slice($arr, 0, $len);
        return implode('', $sub_arr);
    }

    //图片上传
    public function upload_img()
    {
        if ($this->request->isPost()) {
            $file = request()->file('file');
            $path =  $_SERVER['DOCUMENT_ROOT'] . "/uploads/";
            $info = $file->validate(['size' => 1024 * 1024 * 5, 'ext' => 'jpg,png,gif,jpeg'])
                ->move($path);
            if (!$info) {
                return json(['code' => 1, 'status' => 0, 'data' => []]);
                $this->result([], 1, $file->getError());
            } else {
                $url = '/uploads' . '/' . date("Ymd") . '/' . $info->getfileName();
                $url = url($url, '', '', false);
                $url = "https://" . Env('app.api_url') . $url;
                return json(['code' => 0, 'status' => 0, 'data' => ['src' => $url]]);
            }
        }
    }

    public function getsons()
    {
        $data = $this->request->get();
        if (empty($data['classid'])) {
            return json(['status' => 1, 'msg' => '参数信息错误']);
        }
        $list = (new PlatSons())->where(['platclassid' => $data['classid'], 'isshow' => 1])->select();
        return json(['status' => 0, 'data' => $list]);
    }


    public function goodsspec_del()
    {
        $id = trim(input('id'), ',');
        $model = new GoodsSpec();
        $goodsspec = $model->where(['id' => $id, 'isdel' => 1])->find();
        if (empty($goodsspec)) {
            return json(['code' => 1, 'data' => [], 'msg' => '库存规格不存在']);
        }
        //查询规格商品是否超过1个
        $havespec = $model->where(['goodid' => $goodsspec['goodid'], 'isdel' => 1])->count();
        if ($havespec <= 1) {
            return json(['code' => 1, 'data' => [], 'msg' => '库存规格必须保留一个']);
        }
        $res = $model->where(['id' => $id])->update(['isdel' => 2]);
        if ($res) {
            return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'data' => [], 'msg' => '操作失败，请重试']);
    }

    //获取行政
    public function getregion()
    {
        $data = $this->request->get();
        if (empty($data['area_code']) || empty($data['level'])) {
            return json(['status' => 1, 'msg' => '参数信息错误']);
        }
        $pparent = [];
        $parent = (new Region())->where(['level' => ($data['level'] - 1), 'area_code' => $data['area_code']])->find() ?? [];
        if (empty($parent)) {
            return json(['status' => 1, 'data' => [], 'parent' => [], 'msg' => '找不到上级区域']);
        }
        $list = (new Region())->where(['level' => $data['level'], 'parent_id' => $parent['id']])->select();
        if (empty($list)) {
            return json(['status' => 1, 'data' => [], 'parent' => [], 'msg' => '找不到下级区域列表']);
        }
        if ($data['level'] == 4 &&  !empty($data['isget'])) {
            $pparent = (new Region())->where(['level' => ($data['level'] - 2), 'id' => $parent['parent_id']])->find() ?? [];
        }
        return json(['status' => 0, 'data' => $list, 'parent' => $parent, 'pparent' => $pparent]);
    }
}
